package com.kfm.web.controller.system;

import com.kfm.common.domain.entity.Menu;
import com.kfm.common.domain.response.Result;
import com.kfm.system.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @RequestMapping("/list")
    @PreAuthorize("hasAuthority('menu:list') or hasAnyRole('ROLE_admin')")
    public Result list(Menu menu) {
        List<Menu> menus = menuService.selectByCondition(menu);
        return menus == null ? Result.failure("查询失败") : Result.success("查询成功", menus);
    }

    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('menu:delete') or hasAnyRole('ROLE_admin')")
    public Result delete(@PathVariable("id") Integer id) {
        int i = menuService.deleteByPrimaryKey(id);
        return i > 0 ? Result.success("删除成功") : Result.failure("删除失败");
    }

    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('menu:edit') or hasAnyRole('ROLE_admin')")
    public Result edit(@RequestBody Menu menu) {
        int i = menuService.updateByPrimaryKey(menu);
        return i > 0 ? Result.success("修改成功") : Result.failure("修改失败");
    }

    @RequestMapping("/menus")
    public Result getMenus() {
        List<Menu> menus = menuService.selectMenus();
        return menus == null ? Result.failure("查询失败") : Result.success("查询成功", menus);
    }
}
